package com.ygqh.baby.redis;

import com.mumu.commons.redis.sentinel.JedisSentinelSlaveManager;
import com.mumu.commons.redis.sentinel.JedisSentinelSlavePool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Set;

public class RedisSlaveDao {
    private JedisSentinelSlaveManager sentinelSlaveManager;
    private int minIdle;
    private int maxIdle;
    private int maxActive;
    private int maxWait;
    private Boolean testOnBorrow;
    private Boolean testOnReturn;

    private int port;
    private int timeout;
    private String host;
    private String password;

    private String masterName;
    private Set<String> sentinels;


    /**
     * initPoolConfig
     *
     * @return
     */
    private GenericObjectPoolConfig initPoolConfig() {
        GenericObjectPoolConfig jedisPoolConfig = new GenericObjectPoolConfig();
        // 控制一个pool最多有多少个可用的的jedis实例
        jedisPoolConfig.setMaxTotal(maxActive);
        // 最大能够保持空闲状态的对象数
        jedisPoolConfig.setMaxIdle(maxIdle);
        // 超时时间
        jedisPoolConfig.setMaxWaitMillis(maxWait);
        // 在borrow一个jedis实例时，是否提前进行validate操作；如果为true，则得到的jedis实例均是可用的；
        jedisPoolConfig.setTestOnBorrow(false);
        // 在还会给pool时，是否提前进行validate操作
        jedisPoolConfig.setTestOnReturn(false);
        return jedisPoolConfig;
    }

    @PostConstruct
    public void before() {
        GenericObjectPoolConfig jedisPoolConfig = this.initPoolConfig();
        sentinelSlaveManager = new JedisSentinelSlaveManager(masterName, sentinels, jedisPoolConfig, password);
    }


    /**
     * get value from redis
     *
     * @param key
     * @return
     */
    public byte[] get(byte[] key) {
        byte[] value;
        JedisSentinelSlavePool slavePool = sentinelSlaveManager.getSlavePool();
        Jedis jedis = slavePool.getResource();
        try {
            value = jedis.get(key);
        } finally {
            jedis.close();
        }
        return value;
    }


    public String hget(String key, String field) {
        JedisSentinelSlavePool slavePool = sentinelSlaveManager.getSlavePool();
        Jedis jedis = slavePool.getResource();
        String value;
        try {
            value = jedis.hget(key, field);
        } finally {
            jedis.close();
        }
        return value;
    }

    public List<String> hget(String key) {
        JedisSentinelSlavePool slavePool = sentinelSlaveManager.getSlavePool();
        Jedis jedis = slavePool.getResource();
        List<String> value;
        try {
            value = jedis.hvals(key);
        } finally {
            jedis.close();
        }
        return value;
    }

    public List<String> hmget(String key, String... field) {
        JedisSentinelSlavePool slavePool = sentinelSlaveManager.getSlavePool();
        Jedis jedis = slavePool.getResource();
        List<String> value;
        try {
            value = jedis.hmget(key, field);
        } finally {
            jedis.close();
        }
        return value;
    }

    /**
     * set 字符串
     */
    public String get(String key) {
        JedisSentinelSlavePool slavePool = sentinelSlaveManager.getSlavePool();
        Jedis jedis = slavePool.getResource();
        String value = "";
        try {
            value = jedis.get(key);
        } finally {
            jedis.close();
        }
        return value;
    }


    /**
     * keys
     */
    public Set<byte[]> keys(String pattern) {
        Set<byte[]> keys = null;
        JedisSentinelSlavePool slavePool = sentinelSlaveManager.getSlavePool();
        Jedis jedis = slavePool.getResource();
        try {
            keys = jedis.keys(pattern.getBytes());
        } finally {
            jedis.close();
        }
        return keys;
    }

    public int getMinIdle() {
        return minIdle;
    }

    public void setMinIdle(int minIdle) {
        this.minIdle = minIdle;
    }

    public int getMaxIdle() {
        return maxIdle;
    }

    public void setMaxIdle(int maxIdle) {
        this.maxIdle = maxIdle;
    }

    public int getMaxActive() {
        return maxActive;
    }

    public void setMaxActive(int maxActive) {
        this.maxActive = maxActive;
    }

    public int getMaxWait() {
        return maxWait;
    }

    public void setMaxWait(int maxWait) {
        this.maxWait = maxWait;
    }

    public Boolean getTestOnBorrow() {
        return testOnBorrow;
    }

    public void setTestOnBorrow(Boolean testOnBorrow) {
        this.testOnBorrow = testOnBorrow;
    }

    public Boolean getTestOnReturn() {
        return testOnReturn;
    }

    public void setTestOnReturn(Boolean testOnReturn) {
        this.testOnReturn = testOnReturn;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getMasterName() {
        return masterName;
    }

    public void setMasterName(String masterName) {
        this.masterName = masterName;
    }

    public Set<String> getSentinels() {
        return sentinels;
    }

    public void setSentinels(Set<String> sentinels) {
        this.sentinels = sentinels;
    }

}
